<?php
add_action('admin_menu', 'library_quick_add_menu');
function library_quick_add_menu(){
    add_submenu_page('edit.php?post_type=library', __('Quick Add Book','wpbl'), __('Quick Add Book','wpbl'), 10, __FILE__, 'bookSubmit');
}
function drop_tags()
{
    echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
    echo "<option>Tags</option>\n";
    foreach (get_the_tags() as $tag)
    {
        echo "<option value=\"";
        echo get_tag_link($tag->term_id);
        echo "\">".$tag->name."</option>\n";
    }
    echo "</select>";
}
function authors_dropdown() {
    $get_tags = get_terms('authors', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_authors" name="select_authors">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Authors', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option onclick="sendText(document.post.bookauthors, \'%2$s, \');" href="javascript:;" value="%1$s">%2$s</option>', get_term_link($tag->slug, 'authors'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}
function editions_dropdown() {
    $get_tags = get_terms('editions', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_editions" name="select_editions">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Editions', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option onclick="sendText(document.post.bookeditions, \'%2$s, \');" href="javascript:;" value="%1$s">%2$s</option>', get_term_link($tag->slug, 'editions'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}
function publishers_dropdown() {
    $get_tags = get_terms('publishers', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_publishers" name="select_publishers">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Publishers', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option onclick="sendText(document.post.bookpublishers, \'%2$s, \');" href="javascript:;" value="%1$s">%2$s</option>', get_term_link($tag->slug, 'publishers'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}
function classcat_dropdown() {
    $get_tags = get_terms('classcat', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_classcat" name="select_classcat">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Class/Category', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option onclick="sendText(document.post.bookclasscat, \'%2$s, \');" href="javascript:;" value="%1$s">%2$s</option>', get_term_link($tag->slug, 'classcat'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}
function authors_lists() {
    $get_tags = get_terms('authors', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<p>';
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<a onclick="sendText(document.post.bookauthors, \'%2$s, \');" href="javascript:;">%2$s</a>, ', get_term_link($tag->slug, 'authors'), $tag->name);
    endforeach;
    $dropdown_end = '<p>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}
						function wp_insert_myterms(){
						global $post;
						wp_insert_term($my_tax, 'authors');
						wp_insert_term($my_tax_pub, 'publishers');
						wp_insert_term($my_tax_edi, 'editions');
						wp_insert_term($my_tax_cat, 'classcat');
						}

function bookSubmit (){
        if(isset($_POST['submit'])){
                $ok = 1;
                if($_POST['bookid'] == ''){
                        $ok = 0;
                        $check_bookid = 1;
                }

                if($_POST['title'] == ''){
                        $ok = 0;
                        $check_title = 1;
                }

                if($ok){
                        $my_post = array();
                        $my_post['post_title'] = $_POST['title'];
                        $my_post['post_content'] = $_POST['description'];
                        $my_post['post_status'] = 'publish';
                        $my_post['tags_input'] = $_POST['tags'];
                        $my_post['post_category'] = array($_POST['category']);
						$my_post['post_type'] = 'library';
						$my_post['taxonomy_authors'] = $_POST['bookauthors'];
						$my_post['publishers'] = $_POST['bookpublishers'];
						$my_post['classcat'] = $_POST['bookclasscat'];
						$my_post['publishers'] = $_POST['bookpublishers'];
						$my_post['editions'] = $_POST['bookeditions'];
						$my_tax = $_POST['bookauthors'];
						$my_tax_pub = $_POST['bookpublishers'];
						$my_tax_edi = $_POST['bookeditions'];
						$my_tax_cat = $_POST['bookclasscat'];
						$my_bookdate = $_POST['bookdate'];						
                        $id = wp_insert_post($my_post);
                        if($id){

						add_post_meta($id, 'bookid', $_POST['bookid']);
						add_post_meta($id, 'bookdate', $_POST['bookdate']);

?>
                        <div class="wrap"><h2>&nbsp</h2><div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>Book was added successfully.</strong></div></div>
                        <?php
                        }else{
?>
                        <div class="wrap"><h2>&nbsp</h2><div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>There was an error when adding the book.</strong></div></div>
                        <?php
                        }
                        return;
                }
        }

?>
<div class="wrap">

<table cellpadding="5" cellspacing="10">
<tr>
<td width="20%" valign="top">
</td>
<td valign="top" align="left">

</td> 
</tr>
</table>
</div>
<?php
        if(isset($_POST['submit'])){
                if($_POST['bookid'] == ''){
                        echo '<div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>You should enter a valid Book ID</strong></div>';
                }

                if($_POST['title'] == ''){
                        echo '<div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>You should enter a Book title</strong></div>';
                }
	}

?>
<form name="post" method="post">

<div class="wrap">
<h2>Quickly Add New Book into Library</h2>
<div id="poststuff">
<div id="post-body">

<div class="postbox">
<h3>Book Title:</h3>
<div class="inside">
<input type="text" name="title" size="30" tabindex="1" value="<?php echo htmlspecialchars($_POST['title']);?>" id="title"  style="width: 98%;"/>
</div>
</div>

<div class="postbox">
<h3>Book Description:</h3>
<div class="inside">
<textarea id="description" tabindex="2" name="description" cols="40" rows="2" style="width: 98%;"/>
<?php echo htmlspecialchars($_POST['description']);?>
</textarea>
</div>
</div>

<div class="postbox">
<h3>BookISBN or ID: <small>book id!</small></h3>
<div class="inside">
<input type="text" id="bookid" tabindex="3" name="bookid" cols="40" rows="6" style="width: 98%;"/>
<?php echo htmlspecialchars($_POST['bookid']);?>
</input>
</div>
</div>

<div class="postbox">
<h3>Book Author: <small>Book Authors!</small></h3>
<div class="inside">
<label for="bookauthors" style="padding-right:20px;">Type authors name :</label> <input type="text" name="bookauthors" size="30" tabindex="4" value="<?php echo htmlspecialchars($_POST['bookauthors']);?>" id="bookauthors"  style="margin-left:200px;"/><br />or <br /><label>Select from dropdown list</label>
<span style="margin-left:200px;"><?php authors_dropdown(); ?></span>
</div>
</div>

<div class="postbox">
<h3><label for="title">Book Category:</label></h3>
<div class="inside" tabindex="3">
<label for="classcat" style="padding-right:20px;">Type Class/Category name :</label> <input type="text" name="bookclasscat" size="30" tabindex="5" value="<?php echo htmlspecialchars($_POST['bookclasscat']);?>" id="bookclasscat"  style="margin-left:200px;"/><br />or <br /><label>Select from dropdown list</label>
<span style="margin-left:200px;"><?php classcat_dropdown(); ?></span>
</div>
</div>

<div class="postbox">
<h3><label for="title">Book Publishers:</label></h3>
<div class="inside">
<label for="bookpublishers" style="padding-right:20px;">Type Publishers name :</label> <input type="text" name="bookpublishers" size="30" tabindex="6" value="<?php echo htmlspecialchars($_POST['bookpublishers']);?>" id="bookpublishers"  style="margin-left:200px;"/><br />or <br /><label>Select from dropdown list</label>
<span style="margin-left:200px;"><?php publishers_dropdown(); ?></span>
</div>
</div>

<div class="postbox">
<h3><label for="title">Book Editions:</label></h3>
<div class="inside">
<label for="bookeditions" style="padding-right:20px;">Type Edition :</label> <input type="text" name="bookeditions" size="30" tabindex="7" value="<?php echo htmlspecialchars($_POST['bookeditions']);?>" id="bookeditions"  style="margin-left:200px;"/><br />or <br /><label>Select from dropdown list</label>
<span style="margin-left:200px;"><?php editions_dropdown(); ?></span>
</div>
</div>

<div class="postbox">
<h3><label for="title">Book Listed Date:</label></h3>
<div class="inside">
<label for="bookdate" style="padding-right:20px;">Type Current Date or leave blank :</label> <input type="text" name="bookdate" size="30" tabindex="8" value="<?php echo htmlspecialchars($_POST['bookdate']);?>" id="bookdate"  style="margin-left:200px;"/>
</div>
</div>

</div>
</div>

<input type="hidden" name="submit" value="submit">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
?>